﻿using System;
using System.Collections.Generic;
using System.Net.Mail;
using System.Text;

namespace SDK.ApiService
{
    /// <summary>
    /// 发送邮件服务
    /// </summary>
    public class EMailService
    {


        public string eUser { get; set; } = "139939965@qq.com";
        public string eSMTP { get; set; } = "smtp.qq.com";
        public string eUserName { get; set; } = "139939965";
        public string eUserPass { get; set; } = "qlkchdrifhymbigf";


        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="toEmail">指定邮箱</param>
        /// <param name="key">消息类型</param>
        /// <param name="sign">邮件签名</param>
        /// <returns></returns>
        public bool Send(EMailParameter parameter)
        {
            try
            {
                MailAddress from = new MailAddress(this.eUser, parameter.sign, Encoding.UTF8);

                MailAddress toing = new MailAddress(parameter.toEmail);

                MailMessage message = new MailMessage(from, toing);

                // 设置邮件主题 
                message.Subject = parameter.title;

                // 邮件内容使用HTML
                message.IsBodyHtml = true;

                // 设置邮件内容
                message.Body = parameter.body;

                message.BodyEncoding = Encoding.GetEncoding("utf-8");

                SmtpClient client = new SmtpClient(this.eSMTP);

                client.UseDefaultCredentials = false;

                client.EnableSsl = true;

                client.Credentials = new System.Net.NetworkCredential(this.eUserName, this.eUserPass);

                client.Send(message);

                return true;

            }
            catch
            {
                return false;
            }

        }

    }
}
